﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using whatpricemyhouse.Controller;
using whatpricemyhouse.LINQ;
using System.Web.Security;


namespace whatpricemyhouse.agent
{
    public partial class Property : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            new AdminBasicPage();
            ImageButton ImageButton1 = this.LoginView2.FindControl("ImageButton1") as ImageButton;
            ImageButton1.Attributes.Add("onmouseover", "mouseOver1(this);");
            ImageButton1.Attributes.Add("onmouseout", "mouseOut1(this);");
            ImageButton1.Visible = true;
            if (!IsPostBack)
            {
                Calendar Calendar1 = this.LoginView2.FindControl("Calendar1") as Calendar;
                if (Calendar1 != null)
                {
                    Calendar1.SelectedDate = DateTime.Now;
                }

                if (User.Identity.IsAuthenticated)
                {
                    String command = Request.QueryString["comm"];
                    String propertyID = Request.QueryString["pid"];
                    if (!String.IsNullOrEmpty(command))
                    {
                        if (!String.IsNullOrEmpty(propertyID))
                        {
                            switch (command)
                            {
                                case "edit":
                                    reloadProperty(propertyID);
                                    break;
                                case "delete":
                                    deleteProperty(propertyID);
                                    break;
                            }
                        }
                    }
                }
            }




        }

        private void deleteProperty(string propertyID)
        {
            DataControllerDataContext dcdc = new DataControllerDataContext();
            var property = from p in dcdc.market_comparison_property_tables
                           where p.propertyID == propertyID
                           select p;
            foreach (var pp in property)
            {
                dcdc.market_comparison_property_tables.DeleteOnSubmit(pp);
                break;
            }
            dcdc.SubmitChanges();
        }

        private void reloadProperty(string propertyID)
        {
            PlaceSelector ps = this.LoginView2.FindControl("placeSelector") as PlaceSelector;
            TextBox otherLication = this.LoginView2.FindControl("OtherLocationTxt") as TextBox;
            TextBox streetNum = this.LoginView2.FindControl("StreetNumTxt") as TextBox;
            TextBox address = this.LoginView2.FindControl("AddressTxt") as TextBox;
            TextBox status = this.LoginView2.FindControl("StatusTxt") as TextBox;
            Calendar dayOfSale = this.LoginView2.FindControl("Calendar1") as Calendar;
            TextBox price = this.LoginView2.FindControl("PriceTxt") as TextBox;
            TextBox descruption = this.LoginView2.FindControl("DescriptionTxt") as TextBox;
            RadioButtonList imageSelect = this.LoginView2.FindControl("ImageSelectRB") as RadioButtonList;
            HiddenField propertyId = this.LoginView2.FindControl("propertyId") as HiddenField;
            propertyId.Value = propertyID;

            DataControllerDataContext dcdc = new DataControllerDataContext();
            var properties = from p in dcdc.market_comparison_property_tables
                             where p.propertyID == propertyID
                             select p;
            foreach (var pp in properties)
            {
                ps.setSelectedValue(pp.suburbid.ToString());
                otherLication.Text = pp.otherLocation;
                streetNum.Text = pp.streetNum;
                address.Text = pp.address;
                status.Text = pp.status;
                dayOfSale.SelectedDate = pp.dateOfSale.Value;
                price.Text = pp.price.Value.ToString();
                descruption.Text = pp.description;
                imageSelect.SelectedValue = pp.imageUrl;
                break;
            }

            ImageButton ImageButton1 = this.LoginView2.FindControl("ImageButton1") as ImageButton;
            ImageButton1.Visible = false;
            ImageButton UpdateButton = this.LoginView2.FindControl("Update") as ImageButton;
            UpdateButton.Visible = true;


        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            //upload image
            FileUpload fu = this.LoginView2.FindControl("FileUpload1") as FileUpload;
            String imageUrl = "";
            if (fu.HasFile)
            {
                try
                {
                    if (System.IO.Path.GetExtension(fu.FileName) == ".jpg" || System.IO.Path.GetExtension(fu.FileName) == ".png")
                    {


                        Label errorLabel = this.LoginView2.FindControl("UploadFileError") as Label;
                        errorLabel.Text = "";
                        fu.SaveAs(Server.MapPath("../images/upload") + "\\" + fu.FileName);
                        imageUrl = "../images/upload/" + fu.FileName;
                    }
                    else
                    {
                        Label errorLabel = this.LoginView2.FindControl("UploadFileError") as Label;
                        errorLabel.Text = "Must upload Image file .jpg or .png";
                        throw new Exception();
                    }
                }
                catch (Exception ex)
                {

                }
            }


            //get all data

            PlaceSelector ps = this.LoginView2.FindControl("placeSelector") as PlaceSelector;
            TextBox otherLication = this.LoginView2.FindControl("OtherLocationTxt") as TextBox;
            TextBox streetNum = this.LoginView2.FindControl("StreetNumTxt") as TextBox;
            TextBox address = this.LoginView2.FindControl("AddressTxt") as TextBox;
            TextBox status = this.LoginView2.FindControl("StatusTxt") as TextBox;
            Calendar dayOfSale = this.LoginView2.FindControl("Calendar1") as Calendar;
            TextBox price = this.LoginView2.FindControl("PriceTxt") as TextBox;
            TextBox descruption = this.LoginView2.FindControl("DescriptionTxt") as TextBox;
            RadioButtonList imageSelect = this.LoginView2.FindControl("ImageSelectRB") as RadioButtonList;


            DataControllerDataContext dcdc = new DataControllerDataContext();
            market_comparison_property_table mcpt = new market_comparison_property_table();
            mcpt.propertyID = Guid.NewGuid().ToString();

            MembershipUser myObject = Membership.GetUser();
            string UserID = myObject.ProviderUserKey.ToString();

            mcpt.UserId = new Guid(UserID);

            mcpt.suburbid = ps.getSelectedValue();
            mcpt.otherLocation = otherLication.Text;
            mcpt.streetNum = streetNum.Text;
            mcpt.address = address.Text;
            mcpt.status = status.Text;
            mcpt.dateOfSale = dayOfSale.SelectedDate;
            Decimal p = 0;
            try
            {
                p = Decimal.Parse(price.Text);
            }
            catch (Exception ex)
            {

            }
            mcpt.price = p;
            mcpt.description = descruption.Text;
            if (imageUrl != "")
            {
                mcpt.imageUrl = imageUrl;
            }
            else
            {
                mcpt.imageUrl = imageSelect.SelectedValue;
            }
            mcpt.isOld = true;
            mcpt.createDate = DateTime.Now;

            dcdc.market_comparison_property_tables.InsertOnSubmit(mcpt);
            dcdc.SubmitChanges();

            Response.Redirect("~/agent/PropertyComparisonList.aspx");
        }

        protected void Update_Click(object sender, ImageClickEventArgs e)
        {
            HiddenField propertyId = this.LoginView2.FindControl("propertyId") as HiddenField;
            if (propertyId != null)
            {
                //upload image
                FileUpload fu = this.LoginView2.FindControl("FileUpload1") as FileUpload;
                String imageUrl = "";
                if (fu.HasFile)
                {
                    try
                    {
                        if (System.IO.Path.GetExtension(fu.FileName) == ".jpg" || System.IO.Path.GetExtension(fu.FileName) == ".png")
                        {


                            Label errorLabel = this.LoginView2.FindControl("UploadFileError") as Label;
                            errorLabel.Text = "";
                            fu.SaveAs(Server.MapPath("../images/upload") + "\\" + fu.FileName);
                            imageUrl = "../images/upload/" + fu.FileName;
                        }
                        else
                        {
                            Label errorLabel = this.LoginView2.FindControl("UploadFileError") as Label;
                            errorLabel.Text = "Must upload Image file .jpg or .png";
                            throw new Exception();
                        }
                    }
                    catch (Exception ex)
                    {

                    }
                }


                //get all data

                PlaceSelector ps = this.LoginView2.FindControl("placeSelector") as PlaceSelector;
                TextBox otherLication = this.LoginView2.FindControl("OtherLocationTxt") as TextBox;
                TextBox streetNum = this.LoginView2.FindControl("StreetNumTxt") as TextBox;
                TextBox address = this.LoginView2.FindControl("AddressTxt") as TextBox;
                TextBox status = this.LoginView2.FindControl("StatusTxt") as TextBox;
                Calendar dayOfSale = this.LoginView2.FindControl("Calendar1") as Calendar;
                TextBox price = this.LoginView2.FindControl("PriceTxt") as TextBox;
                TextBox descruption = this.LoginView2.FindControl("DescriptionTxt") as TextBox;
                RadioButtonList imageSelect = this.LoginView2.FindControl("ImageSelectRB") as RadioButtonList;


                DataControllerDataContext dcdc = new DataControllerDataContext();

                var properties = from p in dcdc.market_comparison_property_tables
                                 where p.propertyID == propertyId.Value
                                 select p;

                foreach (var mcpt in properties)
                {
                    mcpt.suburbid = ps.getSelectedValue();
                    mcpt.otherLocation = otherLication.Text;
                    mcpt.streetNum = streetNum.Text;
                    mcpt.address = address.Text;
                    mcpt.status = status.Text;
                    mcpt.dateOfSale = dayOfSale.SelectedDate;
                    Decimal p = 0;
                    try
                    {
                        p = Decimal.Parse(price.Text);
                    }
                    catch (Exception ex)
                    {

                    }
                    mcpt.price = p;
                    mcpt.description = descruption.Text;
                    if (imageUrl != "")
                    {
                        mcpt.imageUrl = imageUrl;
                    }
                    else
                    {
                        mcpt.imageUrl = imageSelect.SelectedValue;
                    }
                    break;
                }
                dcdc.SubmitChanges();
                Response.Redirect("~/agent/PropertyComparisonList.aspx");
            }
        }
    }
}